package com.tankbattle.model;

import com.tankbattle.util.ImageManager;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;

/**
 * 爆炸效果类，继承自GameObject
 */
public class Explosion extends GameObject {
    private int currentFrame;    // 当前帧
    private int totalFrames;     // 总帧数
    private int frameDuration;   // 每帧持续时间（毫秒）
    private long lastFrameTime;  // 上一帧的时间
    
    public Explosion(int x, int y) {
        // 爆炸效果大小为32x32，与图片尺寸一致
        super(x, y, 32, 32);
        this.currentFrame = 0;
        this.totalFrames = 3;  // 总共3帧爆炸动画
        this.frameDuration = 100; // 每帧持续100毫秒
        this.lastFrameTime = System.currentTimeMillis();
    }
    
    @Override
    public void update(double delta) {
        long currentTime = System.currentTimeMillis();
        if (currentTime - lastFrameTime > frameDuration) {
            currentFrame++;
            lastFrameTime = currentTime;
            
            // 如果动画播放完毕，将对象设为非活跃
            if (currentFrame >= totalFrames) {
                active = false;
            }
        }
    }
    
    @Override
    public void draw(Graphics2D g) {
        if (!active) return; // 非活跃爆炸不绘制
        
        // 获取当前帧的爆炸图片
        String imageKey = "explosion_" + currentFrame;
        BufferedImage explosionImage = ImageManager.getInstance().getImage(imageKey);
        if (explosionImage != null) {
            g.drawImage(explosionImage, x, y, width, height, null);
        }
    }
}
